<%@page contentType="text/html;charset=utf-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="java.awt.Image"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="com.sun.image.codec.jpeg.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%
//////////////////////////////////////
Calendar calendar = Calendar.getInstance();
//上传文件目录
   File uploadPath = new File(application.getRealPath("/")+"UploadFile");
    if (!uploadPath.exists()) {
       uploadPath.mkdirs();
}
// 临时文件目录
File tempPath = new File(application.getRealPath("/")+"temp");
    if (!tempPath.exists()) {
       tempPath.mkdirs();
    }
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
factory.setSizeThreshold(4096); // 设置缓冲区大小，这里是4kb
factory.setRepository(tempPath);//设置缓冲区目录
upload.setSizeMax(50*1024*1024); // 设置最大文件尺寸，这里是2MB
//////////
String newFileName="";
//////////
try{
List items = upload.parseRequest(request);//得到所有的文件
Iterator itr = null;
if(items!=null)itr = items.iterator();
while (itr!=null&&itr.hasNext()) {
FileItem fi = (FileItem)itr.next();
//检查当前项目是普通表单项目还是上传文件
if (fi.isFormField()) {

}else{
    //得到文件大小
    fi.getSize();
    //得到文件名称
    String fileName = fi.getName();
    //未选择文件时
    if(!fileName.equals("")){
          //获取上传文件的信息
          
          //得到文件类型
          String fileType=fileName.substring(fileName.lastIndexOf(".")+1);
          if(fileType.equalsIgnoreCase("jpg")||fileType.equalsIgnoreCase("gif")||
          fileType.equalsIgnoreCase("jpeg")||fileType.equalsIgnoreCase("png")
          ||fileType.equalsIgnoreCase("flv")||fileType.equalsIgnoreCase("mp4")){
	          //更改文件名，取得当前上传时间的毫秒数值
	          String dateFileName = String.valueOf(calendar.getTimeInMillis());
	          newFileName=dateFileName+"."+fileType;          
	          File saveFile=new File(uploadPath,newFileName);
	          fi.write(saveFile);
	          		}//end fileType
    } //end fileName
}//end fi
}//end while
}catch(Exception e){
}
String uppath="UploadFile/"+newFileName;
out.print(uppath);
%>
